package com.isoft.mapper;

import com.isoft.entity.DfmeDfueEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface DfmeDfueMapper {

    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) " +
            "VALUES(" +
            "#{meta.sender,jdbcType=VARCHAR}, " +
            "#{meta.receiver,jdbcType=VARCHAR}, " +
            "#{meta.sequenceNumber,jdbcType=NUMERIC}, " +
            "TO_TIMESTAMP(#{meta.dateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "#{meta.type,jdbcType=VARCHAR}, " +
            "#{meta.subType,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "msgId", keyColumn = "MSG_ID")
    void insertMeta(DfmeDfueEntity entity);

    @Insert("INSERT INTO FLIGHT_BASE(" +
            "FLIGHT_ID, FLIGHT_ATTRIBUTE, FLIGHT_FILE_ID, FLIGHT_IDENTIFIER) " +
            "VALUES(" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.flightAttribute,jdbcType=VARCHAR}, " +
            "#{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "#{dflt.flightIdentifier,jdbcType=VARCHAR})")
    int insertFlightBase(DfmeDfueEntity entity);

    @Insert({
            "<script>",
            "INSERT INTO FLIGHT_ROUTE(ROUTE_ID, FLIGHT_ID, AIRPORT_CODE, AIRPORT_SEQ, SCHEDULED_TIME, ESTIMATED_TIME, ACTUAL_TIME) ",
            "VALUES ",
            "<foreach collection='dflt.airports' item='airport' separator=','>",
            "(SEQ_ROUTE.nextval, ",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{airport.airportCode,jdbcType=VARCHAR}, ",
            "#{airport.airportSeq,jdbcType=NUMERIC}, ",
            "TO_TIMESTAMP(#{airport.scheduledTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{airport.estimatedTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{airport.actualTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'))",
            "</foreach>",
            "</script>"
    })
    int insertFlightRoute(DfmeDfueEntity entity);
}